<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Response;

class Base extends Controller {

    /**
     * 析构函数
     */
    public function __construct() {
        header("Cache-control: private"); // history.back返回后输入框值丢失问题 参考文章 http://www.tp-shop.cn/article_id_1465.html http://blog.csdn.net/qinchaoguang123456/article/details/29852881
        parent::__construct();
        /*
         * $s = $this->request->request('s');
         * if(!($s == '/admin/index/login' || $s =='/admin/index/login.html') && empty(session('admin_id'))){
         * $this->redirect('/admin/index/login');
         * }
         */
    }

    /*
     * 初始化操作
     */
    public function _initialize() {
    }

    public function needLogin() {
        $id = $this->getLoginId();
        
        if(empty($id)){
            
            if($this->request->isAjax()){
                $this->returnResult(-7,'未登录')->send();
                exit;
            }else{
                return $this->redirect('/admin/index/login'); ;
            }
           
        }
    }

    protected function returnResult($code, $msg = '', $data = '') {
        return json([
            "returnCode" => $code,
            "returnMsg" => $msg,
            "data" => $data
        ]);
    }

    protected function returnListResult($draw, $count, $list, $msg = '') {
        return json(array(
            "returnCode" => 1,
            "returnMsg" => $msg,
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        ));
    }

    protected function getLoginId() {
        return session('ADMIN_KEY');
    }
}
